package test;

import java.util.ArrayList;
import java.util.Scanner;

import compute.DesCbc;
import compute.DesCfb;
import compute.DesEcb;
import compute.DesOfb;
import compute.KeyingOption;
import compute.Mode;
import compute.TdeaCbc;
import compute.TdeaCfb;
import compute.TdeaEcb;
import compute.TdeaOfb;
import data.DataBloc;
import data.Key;

public class DES_Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String line = "";
		System.out.println("");
		String plainText = null;
		Scanner sc = new Scanner(System.in);

		while (!line.equals("q")) {
			while (plainText == null) {
				System.out
						.print("Tapez le texte que vous voulez chiffrer : \t");
				plainText = sc.nextLine();
			}

			String decision2 = null;
			while (decision2 == null) {
				System.out
						.println("Quel algorithme de chiffrement voulez vous utiliser ?");
				System.out.println("\t0 -> DES");
				System.out.println("\t1 -> TDEA");
				System.out.print("Votre choix : \t");
				decision2 = sc.nextLine();
				if (!decision2.equals("0") && !decision2.equals("1")) {
					decision2 = null;
				}
			}

			String decision3 = null;
			while (decision3 == null) {
				System.out.println("Quel mode voulez vous utiliser ?");
				System.out.println("\t0 -> CBC");
				System.out.println("\t1 -> CFB");
				System.out.println("\t2 -> ECB");
				System.out.println("\t3 -> OFB");
				System.out.print("Votre choix : \t");
				decision3 = sc.nextLine();
				if (!decision3.equals("0") && !decision3.equals("1")
						&& !decision3.equals("2") && !decision3.equals("3")) {
					decision3 = null;
				}
			}

			Mode m = null;
			Key[] keys = null;

			if (decision2.equals("0")) {
				keys = new Key[1];
				if (decision3.equals("0")) {
					m = new DesCbc();
					keys[0] = ((DesCbc) m).getKey();
				} else if (decision3.equals("1")) {
					m = new DesCfb();
					keys[0] = ((DesCfb) m).getKey();
				} else if (decision3.equals("2")) {
					m = new DesEcb();
					keys[0] = ((DesEcb) m).getKey();
				} else if (decision3.equals("3")) {
					m = new DesOfb();
					keys[0] = ((DesOfb) m).getKey();
				}
			} else if (decision2.equals("1")) {
				keys = new Key[3];
				if (decision3.equals("0")) {
					m = new TdeaCbc(KeyingOption.KeyingOption1);
					for (int i = 0; i < 3; i++) {
						keys[i] = ((TdeaCbc) m).getKey(i + 1);
					}
				} else if (decision3.equals("1")) {
					m = new TdeaCfb(KeyingOption.KeyingOption1);
					for (int i = 0; i < 3; i++) {
						keys[i] = ((TdeaCbc) m).getKey(i + 1);
					}
				} else if (decision3.equals("2")) {
					m = new TdeaEcb(KeyingOption.KeyingOption1);
					for (int i = 0; i < 3; i++) {
						keys[i] = ((TdeaEcb) m).getKey(i + 1);
					}
				} else if (decision3.equals("3")) {
					m = new TdeaOfb(KeyingOption.KeyingOption1);
					for (int i = 0; i < 3; i++) {
						keys[i] = ((TdeaOfb) m).getKey(i + 1);
					}
				}
			}

			System.out.println();
			System.out
					.println("Vous avez décidé de chiffrer puis de déchiffrer le texte : \""
							+ plainText + "\"");
			System.out.println();
			System.out.println("Les clefs utilisées sont :");
			for (int i = 0; i < keys.length; i++) {
				System.out.print("Clef " + (i + 1) + " : ");
				System.out.println(keys[i].getValue().print());
			}
			System.out.println();

			byte[] bytes = m.encrypt(plainText);
			ArrayList<DataBloc> cipherDataBloc = DataBloc.toDataBlocs(bytes);
			System.out.println("Le texte chiffré est (affichage binaire) : ");
			for (DataBloc d : cipherDataBloc) {
				System.out.println(d.getValue().print());
			}

			System.out.println();
			System.out.print("le texte dechiffré est : \t");

			System.out.println(m.decrypt(bytes));
			plainText = null;
			
			System.out.println();
			System.out.println();
			System.out.println();
			
		}

	}

}
